﻿using KBWebApi.Common;
using KBWebApi.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace KBWebApi.Dal
{
    public class WorkStationDal
    {
        private static SqlSugarScope db = SqlDBHelper.SqlHelper.GetDB();

        public static bool IsExists(WorkStationModel workStation)
        {
            try
            {
                bool isexists = db.Queryable<WorkStationModel>().Any(t => t.XS_WorkStationId == workStation.XS_WorkStationId);
                return isexists;
            }
            catch (Exception ex)
            {
                WriteLog.Writelog("WorkStation.IsExists=>" + ex.ToString());
                return false;
            }
        }

        public int NewKBInput(WorkStationModel workStation)
        {
            int count = db.Insertable(workStation).ExecuteCommand();
            return count;
        }

        public int Update(WorkStationModel workStation)
        {
            int count = db.Updateable(workStation).IgnoreColumns(t => new { t.CreateDate }).ExecuteCommand();
            return count;
        }

        public int Delete(string WorkStationId)
        {
            int count = db.Deleteable<WorkStationModel>().Where(t => t.XS_WorkStationId == int.Parse(WorkStationId)).ExecuteCommand();
            return count;
        }

        public static DataTable GetAllList(string WorkShopId)
        {
            return db.Queryable<WorkStationModel, WorkShopModel>((t, p) => new object[] { JoinType.Left, t.WorkShopId == p.XS_WorkShopId })
                .WhereIF(!string.IsNullOrEmpty(WorkShopId), t => t.WorkShopId == int.Parse(WorkShopId))
                .With(SqlWith.NoLock)
                .OrderBy(t => new { t.XS_WorkStationId, t.WorkShopId, t.CreateDate })
                .Select((t, p) => new { t.XS_WorkStationId, t.WorkShopId, p.WorkShop, t.WorkStation, t.CreateDate })
                .ToDataTable();
        }
    }
}
